﻿using StaffingSystem.Business;
using StaffingSystem.Business.LeaveListBusiness;
using StaffingSystem.EF;
using StaffingSystem.Helper;
using StaffingSystem.Tool;
using StaffingSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace StaffingSystem.Controllers
{
    public class ApplyMangeController : Controller
    {
        Management man = new Management();
        private JsonResult result;

        #region  主页面
        // GET: ApplyMange90
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(PageViewModel page)
        {
            string where = "";
            if (page.Where != null)
            {
                where += page.Where;
            }
            else
            {
                where += string.Empty;
            }
            ProcHepler<View_LeaveList> proc = new ProcHepler<View_LeaveList>();
            //将视图数据映射到业务逻辑数据
            PageModel<View_LeaveList> llist = new PageModel<View_LeaveList>();
            llist.PageNumber = page.PageNumber;
            llist.PageSize = page.PageSize;
            llist.TableName = "View_LeaveList";
            llist.PageKey = "PLID";
            llist.Where = where;
            llist.OrderID = "PLID";
            llist.AscOrDesc = string.Empty;
            llist = proc.GetPageList(llist);

            string data = JsonTool.JsonSerializer(llist);

            result = DataHelper.GetResult(ExecuteState.Succeed, "获取列表成功！", data);
            return result;
        }
        #endregion


        #region  下拉框

        [HttpPost]
        public ActionResult DropType()
        {
            List<LeaveType> tt = new List<LeaveType>();
            tt = man.LeaveType.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(tt)
            };
            return result;
        }
        #endregion


        #region   查看请假信息
        public ActionResult Details(int PLID)
        {
            View_LeaveList lists = man.View_LeaveList.FirstOrDefault(m => m.PLID == PLID);
            return View("Details", lists);
        }
        #endregion


        #region   添加请假信息
        //添加页面信息
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(View_LeaveList model)
        {
            model.ApproveResult = "未审批";

            //实例化方法层  通过存储过程进行数据添加
            LeaveProduce lp = new LeaveProduce();
            int succeed = lp.AddLeaveList(model);
            if (succeed > 0)
            {
                result = DataHelper.GetResult(ExecuteState.Succeed, "添加成功！", "");
            }
            else
            {
                //若不存在此用户 添加失败
                result = DataHelper.GetResult(ExecuteState.Failed, "添加失败！", "");
            }
            return result;
        }
        #endregion


        #region   修改请假信息
        public ActionResult Edit(int PLID)
        {
            //获取信息
            View_LeaveList view = man.View_LeaveList.FirstOrDefault(m => m.PLID == PLID);
            return View("Edit", view);
        }

        [HttpPost]
        public ActionResult Edit(View_LeaveList model)
        {
            try
            {
                //通过存储过程进行数据更改
                LeaveProduce lp = new LeaveProduce();
                int succeed = lp.UpdateLeaveList(model);
                if (succeed > 0)
                {
                    result = DataHelper.GetResult(ExecuteState.Succeed, "修改成功！", "");
                }
                else
                {
                    result = DataHelper.GetResult(ExecuteState.Failed, "修改失败！", "");
                }
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion


        #region   删除请假信息

        [HttpPost]
        public ActionResult Delete(int PLID)
        {
            PersonLeave perl = man.PersonLeave.FirstOrDefault(m => m.PLID == PLID);
            //如果有外检则删除外键表中的数据
            //这里实现外键表数据的删除
            //外键删除后删除要删除的数据
            man.PersonLeave.Remove(perl);
            int rowcount = man.SaveChanges();
            if (rowcount > 0)
            {
                result = DataHelper.GetResult(ExecuteState.Succeed, "删除成功！", "");
            }
            else
            {
                result = DataHelper.GetResult(ExecuteState.Failed, "删除失败！", "");
            }
            return result;
        }
        #endregion
    }
}